package br.ucb.antares.HIB;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import br.ucb.antares.entidade.Cliente;
import br.ucb.antares.interfaces.HIB;
import br.ucb.antares.log4j.ClienteLog;

public class ClienteHIB implements HIB<Cliente>{
	

	public void salvar(Cliente cliente){
		Session session = HibernateUtil.getSession();
		Transaction transaction = session.beginTransaction();
		session.saveOrUpdate(cliente);
		transaction.commit();
		session.close();
		
	}

	public void excluir (Cliente cliente){
		Session session = HibernateUtil.getSession();
		Transaction transaction = session.beginTransaction();
		session.delete(cliente);
		transaction.commit();
		session.close();
		
	}

	@SuppressWarnings("unchecked")
	public List<Cliente>  listar() {
		Session session = HibernateUtil.getSession();
		try {
			return session.createCriteria(Cliente.class).list();
		} finally {
			session.close();
		}
	}
	public Cliente consultar(long id) {
		Session session = HibernateUtil.getSession();
		try {
			Cliente cliente = (Cliente) session.get(Cliente.class, id);
			return cliente;
		} finally {
			session.close();
		}
	}

	public Cliente consultar(String cpf ){
		Session session = HibernateUtil.getSession();
		Criteria crit = session.createCriteria(Cliente .class);
		crit.add(Restrictions.eq("cpf", cpf));
		Cliente cliente = (Cliente ) crit.uniqueResult();
		return cliente;
	}


}
